package com.zerowidth.moc.web.controller;

import com.zerowidth.db.bean.AppConfigBean;
import com.zerowidth.db.bean.NoticeBean;
import com.zerowidth.db.bean.VersionBean;
import com.zerowidth.moc.web.auth.annotation.NoToken;
import com.zerowidth.moc.web.controller.service.VersionService;
import com.zerowidth.moc.web.resp.ResultBean;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1")
public class VersionController {

    @Autowired
    VersionService versionService;

    @NoToken
    @GetMapping("/version")
    public ResponseEntity<ResultBean<VersionBean>> checkNewVersion(
            HttpServletRequest request,
            @RequestParam("currentVersion") String currentVersion) {

        String app_channel = request.getHeader("channel");
        String versionName = request.getHeader("appVersionName");

        return versionService.getLatestVersion(versionName, app_channel, currentVersion);
    }

    @NoToken
    @GetMapping("/config")
    public ResponseEntity<ResultBean<List<AppConfigBean>>> getConfig(
            HttpServletRequest request,
            @RequestParam("force_update") boolean force_update) {

        String app_channel = request.getHeader("channel");

        return versionService.getAppConfigData(app_channel, request, force_update);
    }

    @NoToken
    @GetMapping("/notice")
    public ResponseEntity<ResultBean<List<NoticeBean>>> getNoticeList(
            HttpServletRequest request) {
        String versionName = request.getHeader("appVersionName");
        return versionService.getNoticeList(versionName);
    }

}
